package com.umeox.watch.audiobooks.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.umeox.moto.common.log.Logger;
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.TextView;
import com.umeox.watch.audiobooks.R;
import com.umeox.watch.audiobooks.adapter.AudiobooksAdapter;
import com.umeox.watch.audiobooks.utils.StoryManager;
import com.umeox.watch.audiobooks.utils.StoryManagerKt;
import com.yc.music.model.AudioBean;
import com.yc.music.service.PlayService;
import com.yc.music.tool.BaseAppHelper;

import java.util.List;

/**
 * Created by Rambo 2024/5/20/0020
 * Describe:
 */
public class FavoriteActivity extends ThemeAppCompatActivity implements AudiobooksAdapter.OnClickListener {

    private RecyclerView mStoryRv;
    private List<AudioBean> mStoriesList;
    private AudiobooksAdapter mAdapter;
    private TextView mEmptyView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_favorite_story);
        mStoriesList = StoryManager.INSTANCE.getFavoriteList();
        Logger.e(mStoriesList.toString());
        mStoryRv = findViewById(R.id.rv_stories);
        mEmptyView = findViewById(R.id.tv_story_empty);
        mEmptyView.setVisibility(mStoriesList.isEmpty() ? View.VISIBLE : View.GONE);
        mAdapter = new AudiobooksAdapter(this, R.layout.item_aduio);
        mAdapter.setOnClickListener(this);
        mStoryRv.setAdapter(mAdapter);
        mStoryRv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        mAdapter.setStories(mStoriesList);
        PlayService playService = BaseAppHelper.get().getMusicService();
        if (playService != null) {
            AudioBean playingAudio = playService.getPlayingMusic();
            if (playingAudio != null) {
                mAdapter.setCurAudioId(playingAudio.getId());
            }
        }
    }

    @Override
    public void onItemClick(AudioBean item) {
        Intent intent = new Intent();
        intent.putExtra(StoryManagerKt.INTENT_KEY_LIST_TYPE, StoryManagerKt.LIST_TYPE_FAVORITE);
        String id = item.getId();
        intent.putExtra(StoryManagerKt.INTENT_KEY_AUDIO_ID, id);
        setResult(RESULT_OK, intent);
        finish();
    }
}
